package com.leo.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

/**
 * @author du
 */
public class NioClient {
    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        //Selector selector = Selector.open();

        if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 7000))) {
            while (!socketChannel.finishConnect()) {
                System.out.println("正在连接...");
            }
        }

        ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes(Charset.forName("UTF-8")));
        // 发送数据 (写入channel)
        socketChannel.write(buffer);
        System.in.read();
    }
}
